package com.ibm.spatiotemp.datamodel.datatype;

import java.text.ParseException;

import org.apache.log4j.Logger;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.ibm.spatiotemp.tools.DateParser;
import com.ibm.spatiotemp.tools.jsonParser;

/**
 * for given query json string, find out the actual query type and its
 * corresponding query parameters
 * 
 * @author xuxiaomin
 * 
 */
public class RequestParser {
	public static Logger logger = Logger.getLogger(RequestParser.class);

	public static final int QUERYTYPE_TRAJWITHOUTBBOXANDTR = 0;
	public static final int QUERYTYPE_TRAJWITHBBOXANDTR = 1;
	public static final int QUERYTYPE_FIRSTTIME = 2;
	public static final int QUERYTYPE_POSITIONING = 3;
	public static final int QUERYTYPE_STAINGINGWITHBBOX = 4;
	public static final int QUERYTYPE_STAINGINGWITHOUTBBOX = 5;
	public static final int QUERYTYPE_BUILDINDEX = 6;

	public static String[][] REQUESTTAGS = {
			/* qtype=QUERYTYPE_TRAJWITHOUTBBOXANDTR */
			{ "sessionid", "tenantid", "sourceid", "id", "start", "end",
					"qtype" },
			/* qtype=QUERYTYPE_TRAJWITHBBOXANDTR */
			{ "sessionid", "tenantid", "sourceid", "ids", "start", "end",
					"bbox", "qtype" }, { "sessionid", "tenantid", "sourceid" },
			{ "sessionid", "tenantid", "sourceid" },
			{ "sessionid", "tenantid", "sourceid" },
			{ "sessionid", "tenantid", "sourceid" }, };

	public static class ParsedRequest {
		Integer qtype;
		String sessionid;
		String tenantid;
		String sourceid;

		String id;
		ArrayNode ids;
		Long start;
		Long end;
		JsonNode bbox;

		/**
		 * 
		 * @param jsonstring
		 *            : request string
		 * @throws ParseException
		 */
		public ParsedRequest(String jsonstring) throws ParseException {
			JsonNode jnode = jsonParser.fromString(jsonstring);
			/* required */
			qtype = (jnode.get("qtype").asInt());
			sessionid = jnode.get("sessionid").asText();
			tenantid = jnode.get("tenantid").asText();
			sourceid = jnode.get("sourceid").asText();

			if (qtype == null) {
				logger.error("qtype cannot be null");
				throw new RuntimeException("qtype cannot be null");
			}
			if (sessionid == null) {
				logger.error("sessionid cannot be null");
				throw new RuntimeException("sessionid cannot be null");
			}
			if (tenantid == null) {
				logger.error("tenantid cannot be null");
				throw new RuntimeException("tenantid cannot be null");
			}
			if (sourceid == null) {
				logger.error("sourceid cannot be null");
				throw new RuntimeException("sourceid cannot be null");
			}

			/* optional */
			id = jnode.get("id").asText();
			ids = (ArrayNode) jnode.get("ids");
			start = DateParser.getTimestamp(jnode.get("start").asText());
			end = DateParser.getTimestamp(jnode.get("end").asText());
			bbox = jnode.get("bbox");
		}

		public String getSessionid() {
			return sessionid;
		}

		public String getTenantid() {
			return tenantid;
		}

		public String getSourceid() {
			return sourceid;
		}

		public String getId() {
			return id;
		}

		public ArrayNode getIds() {
			return ids;
		}

		public int getQtype() {
			return qtype;
		}

		public long getStart() {
			return start;
		}

		public long getEnd() {
			return end;
		}

		public JsonNode getBbox() {
			return bbox;
		}

	}

}
